package com.aaa.dang.modules.checkIn.service.impl;

import com.aaa.dang.common.systemlog.annotation.SysLog;
import com.aaa.dang.common.util.ResultModel;
import com.aaa.dang.modules.checkin.dao.PaymentDao;
import com.aaa.dang.modules.checkin.service.IPayment;
import com.aaa.dang.modules.login.service.ILoginService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

import java.util.Map;

/**
 * @author 刘旭泽
 * @version 1.0
 * @date 2018/12/20
 * @description
 */
@Service
public class PaymentImpl implements IPayment {

    @Autowired
    private PaymentDao dao;
    @Autowired
    ILoginService loginService;


    /**
     *@description  交租管理界面获取要展示的所有信息
     *@return com.aaa.dang.common.util.ResultModel
     *@param
     *@author 刘旭泽
     *@date 2018/12/20
     **/
    @Override
    public ResultModel getAllMsg(Map<String, Object> query) {
        ResultModel model = new ResultModel();
        int pageNum = Integer.parseInt(query.get("page")+"");
        int pageSize = Integer.parseInt(query.get("limit")+"");
        PageHelper.startPage(pageNum,pageSize);
        model.setData(dao.getAllMsg(query));
        model.setMsg("");
        model.setCode(0);
        model.setCount(dao.getAllCount(query));

        return model;
    }
    /**
     *@description  把获取到的续租日期添加到数据库记录表中 更改用户到期时间
     *@return com.aaa.dang.common.util.ResultModel
     *@param
     *@author 刘旭泽
     *@date 2018/12/20
     **/
    @Override
    @SysLog(operateType = SysLog.Type.ADD,desc = "用户续租")
    public ResultModel addDateForUser(Map<String, Object> query) {
        String aname =  loginService.getEmployeeName();
        ResultModel model = new ResultModel();
        query.put("aname",aname);
        int a = dao.addDateForUser(query);
        int b = dao.updateUserEndTime(query);
        if(a>0&&b>0){
            model.setCode(0);
            model.setMsg("续租成功");
        }else{
            model.setCode(1);
            model.setMsg("续租失败");
        }
        return model;
    }

    /**
     *@description  获取历史记录
     *@return com.aaa.dang.common.util.ResultModel
     *@param
     *@author 刘旭泽
     *@date 2018/12/20
     **/
    @Override
    public ResultModel getAllHistory(Map<String, Object> query) {
        ResultModel model = new ResultModel();
        int pageNum = Integer.parseInt(query.get("page")+"");
        int pageSize = Integer.parseInt(query.get("limit")+"");
        PageHelper.startPage(pageNum,pageSize);
        model.setData(dao.getAllHistory(query));
        model.setMsg("");
        model.setCode(0);
        model.setCount(dao.getAllHistoryCount(query));
        return model;

    }

    @Override
    public Integer getOverdueCount() {
      List<Map<String,Object>> list = dao.getOverdueCount();
        int i=0;
        for(Map<String,Object> map : list){
            if(map!=null) {
                if (Integer.parseInt(map.get("mytime") + "") < 0) {
                    i++;
                }
            }
        }

       return Integer.valueOf(i);
    }


}
